
package ClientGraphics;

import GameEngine.GameConstants.Graphic;
import GameEngine.Vector;
import Objects.BasicObject;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;

/**
 * Every basic object that is passed to the client, will have a corresponding graphic object
 * which keeps a little extra information relevant to rendering.
 * @author mhfowler
 */
public class GraphicObject implements Comparable {

    /* unique identifier */
    short id;

    /* boolean saying if object exists, used to delete objects (false) */
    boolean exists;

    /* the graphic enumeration of what the object is now, and what it was last game state */
    Graphic former;
    Graphic current;
    /* how many game states has this object had its current graphic enumeration and size */
    int lifetime;

    /* this data structure contains an array of all images this object will cycle betwen */
    Animation anim;

    /* position and size of the object */
    Vector minPos;
    Vector size;
    Vector direction;


    /* this integer represents the layer of this graphic object, where 0 is the background and the higher it is
     * makes it the foreground */
    int layer;

    /* constructor */
    public GraphicObject() {
        lifetime = 0;
        former = null;
        current = null;
        layer = 1;
        direction = new Vector(1,0); // default direction
        exists = true;
    }

    /* method to be called to update graphic object values */
    /* note that it takes in float vectors (from basicObject) and
     * converts them to int vectors */
    public void update(BasicObject o) {
        former = current;
        current = o.getGraph();
        Vector cen = o.getCenter();
        Vector s = o.getSize();
        minPos = new Vector(cen.getX() - (s.getX()/2), cen.getY() - (s.getY()/2));
        size = s;
        direction = o.getDir();
        exists = o.isExists();
        id = o.getId();
    }

    /* gets the image with current cycle of object */
    public ImageWrapper getImage() {
        return anim.getImage();
    }

    /* allows for sorting of graphic objects based on layering */
    @Override
    public int compareTo(Object o) {
        int toReturn = 0;
        GraphicObject toCompare = (GraphicObject) o;
        if (this.layer > toCompare.layer) {
            toReturn = 1;
        }
        else if (this.layer < toCompare.layer) {
            toReturn = -1;
        }
        return toReturn;
    }

}
